package com.zluyuer.ermu.msg;

import java.util.HashMap;
import java.util.Map;

 
public class MessageExecutor {

    private static Map<String, MessageHandler> handlers = new HashMap<String, MessageHandler>();
    
    public static void register(String msgCode, MessageHandler handler) {
        handlers.put(msgCode, handler);
    }
    
    public static void execute(Message msg) throws Exception {
        String msgCode = msg.getMsgCode();
        MessageHandler handler = handlers.get(msgCode);
        if (handler == null) {
            throw new IllegalStateException("Could not find handler for: " + msgCode);
        }
        
        try {
            handler.execute(msg);
        } catch (Exception e) {
            throw new Exception("Error execute msg", e);
        }
    }
}
